package cardmodel

import (
	"context"
	"group-card/common/constant"
	"group-card/common/tool"
	"group-card/model/entity"
	"xorm.io/xorm"
)

var _ CardBindingModel = (*customCardBindingModel)(nil)

type (
	// CardBindingModel is an interface to be customized, add more methods here,
	// and implement the added methods in customCardBindingModel.
	CardBindingModel interface {
		cardBindingModel
		List(ctx context.Context, req *entity.CardBindingReq) ([]*entity.CardBindingResp, int64, error) // 分页查询
		AddCardBindingListDb(ctx context.Context, db *xorm.Session, req []*entity.CardBindingReq) error // 添加卡片绑定记录(事务)
		DeleteCardBindingDb(ctx context.Context, db *xorm.Session, req *entity.CardBindingReq) error    // 删除卡片绑定记录(事务)
		DeleteCardBindingDb1(ctx context.Context, db *xorm.Session, req *entity.CardBindingReq) error   // 删除卡片绑定记录(事务)
	}

	customCardBindingModel struct {
		*defaultCardBindingModel
	}
)

// NewCardBindingModel returns a model for the database table.
func NewCardBindingModel(conn *xorm.Engine) CardBindingModel {
	return &customCardBindingModel{
		defaultCardBindingModel: newCardBindingModel(conn),
	}
}

func (m *defaultCardBindingModel) List(ctx context.Context, req *entity.CardBindingReq) ([]*entity.CardBindingResp, int64, error) {
	sql := m.conn.Table(m.table)

	if req.Columns != "" {
		sql.Select(req.Columns)
	}

	if req.UserId != 0 {
		sql.Where("user_id = ?", req.UserId)
	}

	if req.CardNumber != 0 {
		sql.Where("card_number = ?", req.CardNumber)
	}

	if req.FaceValue != 0 {
		sql.Where("face_value = ?", req.FaceValue)
	}

	if req.Status != 0 {
		sql.Where("status = ?", req.Status)
	} else {
		sql.In("status", []int64{constant.CardBindingStatus20, constant.CardBindingStatus30})
	}

	list := make([]*entity.CardBindingResp, 0)
	count, err := sql.Limit(int(req.PageSize), int((req.PageNum-1)*req.PageSize)).FindAndCount(&list)
	if err != nil {
		tool.GLogLine("获取卡片绑定列表失败 defaultCardBindingModel List:", err)
		return nil, 0, err
	}

	return list, count, err
}

// region TODO: 添加卡片绑定记录(事务) Author:LiuYanLin Date:2023/07/03 周一 13:48
func (m *defaultCardBindingModel) AddCardBindingListDb(ctx context.Context, db *xorm.Session, req []*entity.CardBindingReq) error {
	columns := "card_number,customer_id,face_value,balance,status,service_number"
	has, err := db.Table(m.table).Cols(columns).
		Insert(req)
	if has < 1 || err != nil {
		tool.GLogLine("添加卡片绑定记录失败 defaultCardPublishModel AddCardBindingListDb:", err)
		return err
	}
	return nil
}

//endregion

// region TODO: 删除卡片绑定记录(事务) Author:LiuYanLin Date:2023/07/03 周一 13:48
func (m *defaultCardBindingModel) DeleteCardBindingDb(ctx context.Context, db *xorm.Session, req *entity.CardBindingReq) error {
	has, err := db.Table(m.table).
		Where("service_number = ?", req.ServiceNumber).
		Where("user_id = ?", 0).
		Delete(&entity.CardBinding{})
	if has < 1 || err != nil {
		tool.GLogLine("软删除未绑定的集团卡 defaultCardPublishModel DeleteCardBindingDb:", err)
		return err
	}
	return nil
}

//endregion

// region TODO: 删除卡片绑定记录(事务) Author:LiuYanLin Date:2023/07/03 周一 13:48
func (m *defaultCardBindingModel) DeleteCardBindingDb1(ctx context.Context, db *xorm.Session, req *entity.CardBindingReq) error {
	has, err := db.Table(m.table).
		Where("service_number = ?", req.ServiceNumber).
		Delete(&entity.CardBinding{})
	if has < 1 || err != nil {
		tool.GLogLine("软删除未绑定的集团卡 defaultCardPublishModel DeleteCardBindingDb1:", err)
		return err
	}
	return nil
}

//endregion
